﻿
    using Chain.BLL;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class SysDataBak : PageBase
    {

        private const int GB = 0x40000000;

        private const int KB = 0x400;

        private const int MB = 0x100000;


        public string ByteConversionGBMBKB(long KSize)
        {
            if ((KSize / 0x40000000L) >= 1L)
            {
                return (Math.Round((double) (((float) KSize) / 1.073742E+09f), 2).ToString() + "GB");
            }
            if ((KSize / 0x100000L) >= 1L)
            {
                return (Math.Round((double) (((float) KSize) / 1048576f), 2).ToString() + "MB");
            }
            if ((KSize / 0x400L) >= 1L)
            {
                return (Math.Round((double) (((float) KSize) / 1024f), 2).ToString() + "KB");
            }
            return (KSize.ToString() + "Byte");
        }

        protected void gdvBakList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                FileInfo file = (FileInfo) e.Row.DataItem;
                e.Row.Cells[1].Text = file.FullName.Substring(file.FullName.LastIndexOf(@"\") + 1);
                e.Row.Cells[3].Text = this.ByteConversionGBMBKB(file.Length);
                if (file.Name.Contains("Aoto"))
                {
                    LinkButton lbtn_RDB = (LinkButton) e.Row.FindControl("lbtn_RDB");
                    lbtn_RDB.CommandArgument = string.Format("A|{0}", e.Row.Cells[1].Text);
                    LinkButton lbtn_del = (LinkButton) e.Row.FindControl("lbtn_del");
                    lbtn_del.CommandArgument = string.Format("A|{0}", e.Row.Cells[1].Text);
                    HtmlAnchor dloadfile = (HtmlAnchor) e.Row.FindControl("dloadfile");
                    dloadfile.HRef = "/AppData/AutoDataBase/" + e.Row.Cells[1].Text;
                }
                else
                {
                    LinkButton lbtn_RDB = (LinkButton) e.Row.FindControl("lbtn_RDB");
                    lbtn_RDB.CommandArgument = string.Format("S|{0}", e.Row.Cells[1].Text);
                    LinkButton lbtn_del = (LinkButton) e.Row.FindControl("lbtn_del");
                    lbtn_del.CommandArgument = string.Format("S|{0}", e.Row.Cells[1].Text);
                    e.Row.FindControl("dloadfile");
                    HtmlAnchor dloadfile = (HtmlAnchor) e.Row.FindControl("dloadfile");
                    dloadfile.HRef = "/AppData/DataBase/" + e.Row.Cells[1].Text;
                }
            }
        }

        public void GetBakList()
        {
            string basePath = base.Server.MapPath("../AppData/DataBase/");
            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }
            DirectoryInfo dir = new DirectoryInfo(basePath);
            new StringBuilder();
            FileInfo[] files = dir.GetFiles("*.bak");
            string AutobasePath = base.Server.MapPath("../AppData/AutoDataBase/");
            if (!Directory.Exists(AutobasePath))
            {
                Directory.CreateDirectory(AutobasePath);
            }
            FileInfo[] Autofiles = new DirectoryInfo(AutobasePath).GetFiles("*.bak");
            List<FileInfo> fileList = new List<FileInfo>();
            foreach (FileInfo item in files)
            {
                fileList.Add(item);
            }
            foreach (FileInfo item in Autofiles)
            {
                fileList.Add(item);
            }
            fileList.Sort(delegate (FileInfo f1, FileInfo f2) {
                return f2.LastWriteTime.CompareTo(f1.LastWriteTime);
            });
            this.hidCount.Value = files.Length.ToString();
            this.gdvBakList.DataSource = fileList;
            this.gdvBakList.DataBind();
            PageBase.BindSerialGridView(this.gdvBakList, false, 0);
        }

        protected void GV_CommandItem(object sender, GridViewCommandEventArgs e)
        {
            string[] info = e.CommandArgument.ToString().Split(new char[] { '|' });
            string fileinfopath = "";
            string name3 = e.CommandName.ToString();
            if (name3 == null)
            {
                goto Label_0104;
            }
            if (!(name3 == "RETDB"))
            {
                if (name3 == "DELDB")
                {
                    string name1 = info[0];
                    if (name1 == null)
                    {
                        return;
                    }
                    if (!(name1 == "A"))
                    {
                        if (!(name1 == "S"))
                        {
                            return;
                        }
                    }
                    else
                    {
                        fileinfopath = base.Server.MapPath("../AppData/AutoDataBase/");
                        goto Label_00CE;
                    }
                    fileinfopath = base.Server.MapPath("../AppData/DataBase/");
                    goto Label_00CE;
                }
                goto Label_0104;
            }
            if (this.RETB_SQL(info[0], info[1]))
            {
                base.OutputWarn("还原成功！");
                return;
            }
            base.OutputWarn("还原失败！");
            return;
        Label_00CE:
            if (File.Exists(fileinfopath + info[1]))
            {
                File.Delete(fileinfopath + info[1]);
                base.OutputWarn("删除成功！");
                return;
            }
            base.OutputWarn("文件不存在！");
            return;
        Label_0104:
            base.OutputWarn("您未提交命令！");
        }

        public void Log(string logText)
        {
            string fileName = string.Concat(new object[] { DateTime.Now.ToShortDateString().Replace("-", ""), "_", DateTime.Now.Hour, "_", DateTime.Now.Minute / 10 });
            string fullPath = base.Server.MapPath("~/Upload/" + fileName + ".txt");
            logText = DateTime.Now.ToString() + "\r\n" + logText;
            File.AppendAllText(fullPath, logText + "\r\n\r\n");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.txtBakName.Value = new Chain.BLL.SysParameter().GetDataBaseName();
                this.GetBakList();
            }
        }

        private bool RETB_SQL(string type, string file)
        {
            try
            {
                string fileinfopath = string.Empty;
                string name3 = type;
                if (name3 == null)
                {
                    goto Label_0050;
                }
                if (!(name3 == "A"))
                {
                    if (name3 == "S")
                    {
                        goto Label_003D;
                    }
                    goto Label_0050;
                }
                fileinfopath = base.Server.MapPath("../AppData/AutoDataBase/");
                goto Label_0055;
            Label_003D:
                fileinfopath = base.Server.MapPath("../AppData/DataBase/");
                goto Label_0055;
            Label_0050:
                return false;
            Label_0055:
                if (File.Exists(fileinfopath + file))
                {
                    Chain.BLL.SysParameter par = new Chain.BLL.SysParameter();
                    try
                    {
                        par.ReductionDataBakUp(fileinfopath + file);
                    }
                    catch (Exception e)
                    {
                        this.Log(e.ToString());
                        return false;
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                PubFunction.SaveSysLog(-1, -1, "系统标志", e.ToString(), -1, DateTime.Now, PubFunction.ipAdress);
                return false;
            }
        }
    }


